import { Component, OnInit } from '@angular/core'
import { Router, ActivatedRoute } from '@angular/router' //路由引入
import { JsonApiService } from 'app/core/api/json-api.service'
import { config } from '../../shared/smartadmin.config'

declare var $: any
declare var Viewer: any
@Component({
  selector: 'sa-fault-page',
  templateUrl: './fault-page.component.html',
  styleUrls: ['./fault-page.css']
})
export class FaultPageComponent implements OnInit {
  module: any = this.module ? this.module : {}
  pageType: string = 'new' //页面类型,新增:new  编辑:edit
  curPage = 1
  editparamId = '' //编辑内容的id
  carNumber: string = '' //车牌号码
  deviceId: string = ''
  isPush: string = '0'
  restrictTime: string = ''
  faultType: string = '1' //故障类型 1:传感器 2:GPS 3:手刹
  faultTime: string = '' //故障时间
  editGuide: string = '' //维修指导
  remark: string = '' //备注
  gpsAntenna: string = '' //gps天线
  gsmAntenna: string = '' //gsm天线
  editRecord = [{ content: '' }] //维修记录数据
  faultSatatus = '0' //故障状态
  editStaff = '' //维修人员
  editTime = '' //维修时间
  repairedConclusion = '' //维修总结
  faultItemlist: any = [{ type: '1', phenomenon: '1', typeText: '', phenText: '' }] //故障现象明细数据数组
  phenTypeArr = [] //故障现象类型选项         例:传感器1
  phenValueArr = [] //故障现象选项值    例:最大正值
  afterImageInfo: any = [] //维修后图片
  beforeImageInfo: any = [] //维修前图片
  otherImageInfo: any = [] //维修其他图片
  //	driverPhone:string='';
  sensorType: any = [
    { type: 1, text: '传感器1' },
    { type: 2, text: '传感器2' },
    { type: 3, text: '传感器3' },
    { type: 4, text: '传感器4' },
    { type: 5, text: '传感器5' },
    { type: 6, text: '传感器6' },
    { type: 7, text: '传感器7' },
    { type: 8, text: '传感器8' },
    { type: 9, text: '传感器9' },
    { type: 10, text: '传感器10' }, //传感器类型
    { type: 11, text: '传感器11' },
    { type: 12, text: '传感器12' },
    { type: 13, text: '传感器13' },
    { type: 14, text: '传感器14' },
    { type: 15, text: '传感器15' },
    { type: 16, text: '传感器16' }
  ]
  faultTypeArr: any = [
    { type: 1, text: '传感器' }, //故障类型选项
    { type: 2, text: 'GPS' },
    { type: 3, text: '手刹' },
    { type: 4, text: '主机' },
    { type: 5, text: '线材' }
  ]
  sensorPhen: any = [
    { type: 1, text: '平直' }, //传感器故障现象选项
    { type: 2, text: '正最大值' },
    { type: 3, text: '负最大值' },
    { type: 4, text: '乱跳' },
    { type: 5, text: '零值' }
  ]
  GpsArr = [{ type: 1, text: 'GPS坐标' }]
  GpsPhen: any = [
    { type: 1, text: '丢失' }, //GPS故障现象选项
    { type: 2, text: '漂移' }
  ]
  HandbrakeArr = [{ type: 1, text: '手刹' }]
  HandbrakePhen = [{ type: 1, text: '信号无变化' }] //手刹信号故障现象选项
  editGuideArr: any = [{ type: 1, text: '更换传感器' }] //维修指导选项
  hostArr = [{ type: 1, text: '主机' }]
  hostPhen = [{ type: 1, text: '离线' }] //主机故障现象选项
  lineArr = [{ type: 1, text: '线材' }] //线材选项
  linePhen = [{ type: 1, text: '无通道' }, { type: 2, text: '全部平直' }, { type: '过磅传感器不跳' }] //线材现象
  userList = [] //所有维修人员
  imgList = [] //fr保存图片
  imgFileList = [] //fd上传图片
  pathsList: any = '' //从后台获取的图片
  isArr: boolean = false //从后台获取的图片格式是否是数组
  editFlag: boolean = true
  isPushTime: number = 6
  // gps选项
  gpsOptions = allSelectOptionsObj.gpsOptions
  // gsm选项
  gsmOptions = allSelectOptionsObj.gsmOptions
  constructor(private router: Router, private activatedRoute: ActivatedRoute, private jsonApiService: JsonApiService) {
    activatedRoute.queryParams.subscribe(queryParams => {
      this.pageType = queryParams.type
      this.curPage = queryParams.nowPage ? queryParams.nowPage : 1
      this.editparamId = queryParams.id ? queryParams.id : ''
    })
  }
  faultTypeChange() {
    this.faultItemlist = [{ type: '1', phenomenon: '1', typeText: '', phenText: '' }]
    if (this.faultType == '1') {
      this.phenTypeArr = this.sensorType
      this.phenValueArr = this.sensorPhen
    }
    if (this.faultType == '2') {
      this.phenTypeArr = this.GpsArr
      this.phenValueArr = this.GpsPhen
    }
    if (this.faultType == '3') {
      this.phenTypeArr = this.HandbrakeArr
      this.phenValueArr = this.HandbrakePhen
    }
    if (this.faultType == '4') {
      this.phenTypeArr = this.hostArr
      this.phenValueArr = this.hostPhen
    }
    if (this.faultType == '5') {
      this.phenTypeArr = this.lineArr
      this.phenValueArr = this.linePhen
    }
    this.faultItemlist[0].typeText = this.phenTypeArr[Number(this.faultItemlist[0].type) - 1].text
    this.faultItemlist[0].phenText = this.phenValueArr[Number(this.faultItemlist[0].phenomenon) - 1].text
  }
  //拉取车辆数据
  select2Car() {
    var that = this
    return new Promise(function (resolve, reject) {
      System.import('script-loader!select2/dist/js/select2.min.js').then(() => {
        System.import('assets/js/select2-zh-CN.js').then(() => {
          $('#carNumberSelect2')
            .select2({
              language: 'zh-CN',
              placeholder: '请选择',
              allowClear: true,
              minimumInputLength: 2, //最少2个字符，select2才显示查询的内容
              ajax: {
                url: config.BaseURL + 'stats/carListByCarNumber.html',
                dataType: 'json',
                delay: 50,
                method: 'POST',
                xhrFields: {
                  withCredentials: true
                },
                data: function (params) {
                  return {
                    carNumber: params.term // search term
                  }
                },
                processResults: function (data, params) {
                  var options = new Array()
                  // 小于10个数量才判断重复
                  if (data.data.length <= 10) {
                    let arr = []
                    let sameCar = []
                    // 判断相同的车牌号
                    for (let index = 0; index < data.data.length; index++) {
                      let car = data.data[index]
                      if (arr.indexOf(car.carNumber) != -1) {
                        sameCar.push(car.carNumber)
                      } else {
                        arr.push(car.carNumber)
                      }
                    }
                    $(data.data).each(function (index, car) {
                      if (sameCar.indexOf(car.carNumber) != -1) {
                        options.push({
                          id: car.deviceId,
                          text: car.carNumber + '[' + car.parentName + ']'
                        })
                      } else {
                        options.push({
                          id: car.deviceId,
                          text: car.carNumber
                        })
                      }
                    })
                  } else {
                    $(data.data).each(function (index, car) {
                      options.push({
                        id: car.deviceId,
                        text: car.carNumber
                      })
                    })
                  }
                  return {
                    results: options //获取查询后的结果
                  }
                },
                cache: true
              }
            })
            .on('select2:select', function (e) {
              var data = e.params.data
              that.deviceId = data.id
              if (data.text.indexOf('[') > 0) {
                that.carNumber = data.text.split('[')[0]
              } else {
                that.carNumber = data.text
              }
            })
          resolve()
        })
      })
    })
  }

  //初始化时间
  initDate() {
    var that = this
    return new Promise(function (resolve, reject) {
      System.import('assets/js/datepicker/js/bootstrap-datetimepicker.min.js').then(() => {
        System.import('assets/js/datepicker/locales/bootstrap-datetimepicker.zh-CN.js').then(() => {
          $('#datetimepicker1')
            .datetimepicker({
              //时间插件
              format: 'yyyy-mm-dd hh:mm:ss',
              language: 'zh-CN',
              autoclose: true
            })
            .on('changeDate', function (e) {
              var date = e.date
              var time = common.getTimeMmss(date)
              that.faultTime = time
            })
          var date1 = common.getTimeMmss(new Date())
          $('#datetimepicker1').val(date1) //当前时间
          that.faultTime = date1
          resolve()
        })
      })
    })
  }

  initFaultTime() {
    var that = this
    return new Promise(function (resolve, reject) {
      System.import('assets/js/datepicker/js/bootstrap-datetimepicker.min.js').then(() => {
        System.import('assets/js/datepicker/locales/bootstrap-datetimepicker.zh-CN.js').then(() => {
          $('#editTime')
            .datetimepicker({
              //时间插件
              format: 'yyyy-mm-dd hh:mm:ss',
              language: 'zh-CN',
              autoclose: true
            })
            .on('changeDate', function (e) {
              var date = e.date
              var time = common.getTimeMmss(date)
              that.editTime = time
            })
          var date1 = common.getTimeMmss(new Date())
          $('#editTime').val(date1) //当前时间
          that.editTime = date1
          resolve()
        })
      })
    })
  }
  //添加故障现象事件
  item_add() {
    var typeStr = this.phenTypeArr[0].text //默认添加第一个选项
    var phenStr = this.phenValueArr[0].text
    var item = { type: '1', phenomenon: '1', typeText: typeStr, phenText: phenStr }
    this.faultItemlist.push(item)
    //		this.selectReset();
  }
  //删除故障现象事件
  item_del(sort) {
    this.faultItemlist.splice(sort, 1)
  }
  //添加维修记录事件
  editItem_add() {
    var item = { content: '' }
    this.editRecord.push(item)
  }
  //删除维修记录事件
  editItem_del(sort) {
    this.editRecord.splice(sort, 1)
  }
  //gps天线change回调
  gpsAntennaChange(val) {
    this.gpsAntenna = val
    //   if(this.gsmAntenna!=val){
    // 	  this.gsmAntennaChange(val)
    //   }
    for (var i = 0; i < allSelectOptionsObj.gsmOptions.length; i++) {
      if (allSelectOptionsObj.gsmOptions[i].text == val) {
        this.gsmAntenna = val
      }
    }
  }
  //gsm天线change回调
  gsmAntennaChange(val) {
    this.gsmAntenna = val
    //   if(this.gpsAntenna!=val){
    // 	this.gpsAntennaChange(val)
    // }
    for (var i = 0; i < allSelectOptionsObj.gpsOptions.length; i++) {
      if (allSelectOptionsObj.gpsOptions[i].text == val) {
        this.gpsAntenna = val
      }
    }
  }
  //全局请求 天线配置接口 GPS天线/GSM天线
  getGpsInfo() {
    this.jsonApiService.http('gpsAntenna/getGpsInfo.html', '').subscribe(data => {
      if (data.success) {
        var obj
        allSelectOptionsObj.gpsOptions = []
        allSelectOptionsObj.gsmOptions = []
        for (var i = 0; i < data.data.length; i++) {
          if (data.data[i].type == 'GPS天线') {
            obj = {
              value: data.data[i].antennaName,
              text: data.data[i].antennaName
            }
            allSelectOptionsObj.gpsOptions.push(obj)
          } else if (data.data[i].type == 'GSM天线') {
            obj = {
              value: data.data[i].antennaName,
              text: data.data[i].antennaName
            }
            allSelectOptionsObj.gsmOptions.push(obj)
          }
        }
        this.gpsOptions = allSelectOptionsObj.gpsOptions
        this.gsmOptions = allSelectOptionsObj.gsmOptions
      }
    })
  }
  submitEvent() {
    if (this.deviceId == '') {
      this.jsonApiService.message('请选择车辆...')
      return
    }
    var editRecords = []
    for (let item of this.editRecord) {
      if (common.stringTrim(item.content)) {
        editRecords.push(item.content)
      }
    }
    let getFaultMes = this.getSelectedVAL()
    if (this.seneorPhenlist.length == 0) {
      if (this.sensorPhenName && getFaultMes.resultPhenStr) {
        this.addseneorPhenItem()
        getFaultMes = this.getSelectedVAL()
      }
    }
    if (!getFaultMes.problemClassStr) {
      this.jsonApiService.message('故障分类不能为空')
      return
    }
    this.beforeImageInfo = this.filter(this.beforeImageInfo)
    this.afterImageInfo = this.filter(this.afterImageInfo)
    let isPush = this.isPush == 'time' ? '0' : this.isPush
    let isPushTime = this.isPush == 'time' && !this.restrictTime ? this.isPushTime : ''
    let restrictTime = this.isPush == 'time' && this.restrictTime ? this.restrictTime : ''
    var submitObj = {
      id: undefined,
      deviceId: this.deviceId, //编号
      carNumber: this.carNumber, //车牌
      faultTypeName: getFaultMes.problemClassStr, //故障类型
      faultTime: this.faultTime, //故障时间
      faultPhenomenon: getFaultMes.resultPhenStr ? getFaultMes.resultPhenStr : '', //故障现象:例  传感器1-平直
      repairedGuide: getFaultMes.repairGuideStr, //维修指导
      repairedRecords: editRecords, //维修记录
      repairedStatus: this.faultSatatus, //维修状态(故障状态)
      repairedTime: this.editTime, //维修时间
      repairedUser: this.editStaff, //维修人员
      remark: this.remark, //备注
      repairedConclusion: this.repairedConclusion, //维修总结
      isPush: isPush,
      time: isPushTime,
      restrictTime: restrictTime,
      gsmAntenna: this.gsmAntenna,
      gpsAntenna: this.gpsAntenna,
      beforeImageInfoJson: JSON.stringify(this.beforeImageInfo), //备注
      afterImageInfoJson: JSON.stringify(this.afterImageInfo) //备注
      //			driverPhone:this.driverPhone,					//司机电话
    }
    if (!submitObj.faultTime) {
      this.jsonApiService.message('故障时间不能为空')
      return
    } else {
      var isDate = common.checkDateFormat(submitObj.faultTime)
      if (!isDate) {
        this.jsonApiService.message('故障时间格式输入不正确')
        return
      }
    }
    if (!submitObj.repairedTime && this.faultSatatus == '1') {
      this.jsonApiService.message('维修时间不能为空')
      return
    }
    if (this.faultSatatus !== '1') {
      submitObj.repairedTime == ''
    }
    if (submitObj.repairedTime) {
      var isDate = common.checkDateFormat(submitObj.repairedTime)
      if (!isDate) {
        this.jsonApiService.message('维修时间格式输入不正确')
        return
      }
    }
    let apiurl = 'deviceRepaired/insertDeviceRepaired.html'
    if (this.editparamId == '') {
      submitObj.repairedTime = ''
    } else {
      submitObj.id = this.editparamId
      apiurl = 'deviceRepaired/updateDeviceRepaired.html'
    }
    const fm = new FormData()
    this.imgFileList.forEach(item => {
      fm.append('files', item)
    })
    for (var item in submitObj) {
      // if(submitObj[item]){
      fm.append(item, submitObj[item])
      // }
    }

    this.jsonApiService.form_http(apiurl, fm).subscribe(result => {
      this.jsonApiService.message(result.message)
      if (result.success) {
        this.goBack()
      }
    })
  }
  initAllUser() {
    var that = this
    return new Promise(function (resolve, reject) {
      that.jsonApiService.http('user/getAllUserList.html', null).subscribe(data => {
        if (data.success) {
          var userList = data.data
          var selectdata = []
          for (var i = 0; i < userList.length; i++) {
            var obj = {
              id: userList[i].id,
              text: userList[i].name
            }
            selectdata[i] = obj
          }
        }
        that.userList = selectdata
        resolve()
      })
    })
  }
  selectedChange(el, type, params) {
    var index = Number($(el.target).val()) - 1
    if (type == 'type') {
      params.type = index + 1 + ''
      params.typeText = this.phenTypeArr[index].text
    }
    if (type == 'phenomenon') {
      params.phenomenon = index + 1 + ''
      params.phenText = this.phenValueArr[index].text
    }
    if (type == 'editType') {
      params.editType = index + 1
    }
  }
  ngOnInit() {
    this.getGpsInfo() //请求天线配置接口
    this.initProTypeOptions()
    this.select2Car().then(() => {
      this.getEditMessage()
    })
    this.initDate()
    if (this.editparamId !== '') {
      this.initFaultTime()
    }
    this.selectReset()
    this.faultTypeChange()
    this.editFlag = false

    if (this.pageType == 'new') {
      this.afterImageInfo = JSON.parse(JSON.stringify(this.imageInfo))
      this.beforeImageInfo = JSON.parse(JSON.stringify(this.imageInfo))
    }
  }
  getEditMessage() {
    if (this.editparamId) {
      this.initAllUser()
      let reqdata = 'id=' + this.editparamId
      const self = this
      this.jsonApiService.http('deviceRepaired/getDeviceRepairedById.html', reqdata).subscribe(result => {
        //				this.driverPhone=result.data.driverPhone;
        this.deviceId = result.data.deviceId //编号
        this.carNumber = result.data.carNumber //车牌
        this.gpsAntenna = result.data.gpsAntenna
        this.gsmAntenna = result.data.gsmAntenna
        this.isPush = result.data.restrictTime ? 'time' : result.data.isPush + ''
        this.restrictTime = result.data.restrictTime
        this.pathsList = result.data.paths
        this.isArr = Array.isArray(this.pathsList)
        this.editFlag = true
        if (this.isArr) {
          this.pathsList.forEach(item => {
            self.imgList.push({ name: '', imgUrl: item }) //获取上传的图片
          })
        }

        setTimeout(
          function () {
            $('#carNumberSelect2').val(this.deviceId).trigger('change')
            var str = $("<span class='select2-selection__rendered'>" + this.carNumber + '</span>')
            $('#carNumberSelect2').val('')
            $('#select2-carNumberSelect2-container').empty()
            $('#select2-carNumberSelect2-container').attr('title', '')
            $('#select2-carNumberSelect2-container').prepend(str)
          }.bind(this),
          50
        )

        //				for(let i=0;i<this.faultTypeArr.length;i++){
        //					if(this.faultTypeArr[i].text==result.data.faultTypeName){
        //						this.faultType=(i+1)+'';
        //						this.faultTypeChange();
        //						break;
        //					}
        //				}
        this.faultTime = result.data.faultTime //故障时间
        //				this.faultItemlist=[];
        //				for(let item of result.data.faultPhenomenon){
        //					var itemArr=item.split('-');
        //					var obj={type:'',phenomenon:'',typeText:itemArr[0],phenText:itemArr[1]}
        //					for (let _item of this.phenTypeArr){
        //						if(_item.text==itemArr[0]){
        //							obj.type=_item.type;
        //							break;
        //						}
        //					}
        //					for (let _item of this.phenValueArr){
        //						if(_item.text==itemArr[1]){
        //							obj.phenomenon=_item.type;
        //							break;
        //						}
        //					}
        //					this.faultItemlist.push(obj);        		//故障现象:例  传感器1-平直
        //				}
        //				this.selectReset();
        //				var guideStr=result.data.repairedGuide[0];	//后台传过来的维修指导字符串
        //				if(guideStr=="更换传感器"||guideStr=="更换手刹"||guideStr=="更换天线"||guideStr=="更换线材"){
        //					this.editGuide='1';							//维修指导
        //				}
        //				else if(guideStr=="更换主机"){
        //					this.editGuide='2';
        //				}
        this.faultSatatus = result.data.repairedStatus //维修状态(故障状态)
        this.editTime = result.data.repairedTime ? result.data.repairedTime : '' //维修时间
        this.editStaff = result.data.repairedUser //维修人员
        this.remark = result.data.remark //备注
        this.repairedConclusion = result.data.repairedConclusion //维修总结
        if (result.data.repairedRecords) {
          if (result.data.repairedRecords.length > 0) {
            //维修记录
            this.editRecord = []
            for (let item of result.data.repairedRecords) {
              this.editRecord.push({ content: item })
            }
          }
        }
        this.proTypeParams.problemType = '1'
        this.proSlectedItem = this.proTypeOptions[1]
        let typeStr = result.data.faultTypeName
        let phenStr = result.data.faultPhenomenon && result.data.faultPhenomenon[0]
        if (typeStr == '传感器问题' || typeStr == '传感器') {
          this.proTypeParams.problemClass = '1'
          this.seneorPhenlist = []
          let len = result.data.faultPhenomenon.length
          for (let i = 0; i < len; i++) {
            let item = result.data.faultPhenomenon[i].split(',')
            let pItem = item[0].split('-') //将"传感器6-正负52万" 格式拆分出来
            let pheItem = {
              index: i,
              sensorName: pItem[0],
              phenomenon: pItem[1]
            }
            this.seneorPhenlist.push(pheItem)
            if (i == len - 1) {
              this.sensorPhenName = item[0].substring(3, 4)
              let data = this.proTypeOptions[1].childs[1].phenomenon
              for (let j = 0; j < data.length; j++) {
                if (pItem[1] == data[j].name) {
                  this.proTypeParams.phenomenon = j + ''
                  break
                }
              }
            }
          }
        } else {
          if (typeStr == '主机问题' || typeStr == '主机') {
            this.proTypeParams.problemClass = '0'
            this.proTypeParams.phenomenon = problemOptions.findPhenomenonbyStr(0, phenStr) + ''
          }
          if (typeStr == 'GPS问题' || typeStr == 'GPS') {
            this.proTypeParams.problemClass = '2'
            this.proTypeParams.phenomenon = problemOptions.findPhenomenonbyStr(2, phenStr) + ''
          }
          if (typeStr == '其他问题' || typeStr == '其他') {
            this.pcIsOther = true
            this.otherInpMess = phenStr
            this.proTypeParams.problemClass = '4'
            this.proTypeParams.phenomenon = problemOptions.findPhenomenonbyStr(4, phenStr) + ''
          }
          if (typeStr == '线材问题' || typeStr == '线材') {
            //						this.pcIsOther=true;
            //						this.otherInpMess=phenStr;
            this.proTypeParams.problemClass = '3'
            this.proTypeParams.phenomenon = problemOptions.findPhenomenonbyStr(3, phenStr) + ''
          }
        }

        let cls = +this.proTypeParams.problemClass
        let peh = +this.proTypeParams.phenomenon
        if (result.data.repairedGuide) {
          if (result.data.repairedGuide.length > 0) {
            this.proTypeParams.repairGuide =
              problemOptions.findRepairGuidebyStr(cls, peh, result.data.repairedGuide[0]) + ''
          }
        }

        //显示图片
        if (result.data.afterImageInfo && result.data.afterImageInfo.length > 0) {
          this.show_img('after', result.data.afterImageInfo)
        }
        if (result.data.afterImageInfo && result.data.afterImageInfo.length > 0) {
          for (let i = 0; i < result.data.afterImageInfo.length; i++) {
            if (result.data.afterImageInfo[i].index == 6) {
              this.show_img('other', result.data.afterImageInfo[i].imageList)
            }
          }
        }
        if (result.data.beforeImageInfo && result.data.beforeImageInfo.length > 0) {
          this.show_img('before', result.data.beforeImageInfo)
        }
      })
    }
  }
  selectReset() {
    setTimeout(
      function () {
        $('.modelSelect').each(function () {
          var data = $(this).attr('data')
          $(this).val(data)
        })
      }.bind(this),
      10
    )
  }
  goBack() {
    if (this.curPage < 2) {
      this.router.navigate(['/device-management/fault'])
    } else {
      this.router.navigate(['/device-management/fault'], { queryParams: { nowPage: this.curPage } })
    }
  }

  proTypeOptions = [] //存储问题记录分类选项描述
  proSlectedItem: any //记录选中一类时的数据,渲染页面选项
  sensorPhenName = ''
  proTypeParams = {
    //存储用户操作选择的数据
    problemType: '', //问题类型
    problemClass: '', //问题分类
    phenomenon: '', //问题现象
    repairGuide: '', //维修指导
    repairWay: '', //维修方式
    pheDescribe: '' //现象描述
  }
  seneorPhenlist = []
  startTime_problem: string = ''
  endTime_problem: string = ''

  initProTypeOptions() {
    this.proTypeOptions = problemOptions.initProTypeOptions
    this.proTypeParams.problemType = '1'
    this.proSlectedItem = this.proTypeOptions[1]
  }
  //监听问题类型改变事件
  problemTypeChange() {
    if (this.proTypeParams.problemType == '') return
    let sort = +this.proTypeParams.problemType
    this.proSlectedItem = this.proTypeOptions[sort]
    this.proTypeParams.problemClass = ''
    this.problemClassChange()
  }
  //监听问题分类改变事件
  pcIsOther = false
  otherInpMess = ''
  problemClassChange() {
    this.seneorPhenlist = []
    this.proTypeParams.phenomenon = ''
    this.otherInpMess = ''
    this.isPush = '0'
    //		判断选的是不是其他
    let isOther = this.proSlectedItem.childs[+this.proTypeParams.problemClass].name
    if (isOther == '其他') {
      this.pcIsOther = true
    } else {
      this.pcIsOther = false
    }
    if (this.proTypeParams.problemType == '1' && this.proTypeParams.problemClass == '1') {
    } else {
      this.problemPhenChange()
    }
  }
  //监听问题现象改变事件
  problemPhenChange() {
    this.proTypeParams.repairGuide = ''
    this.proTypeParams.repairWay = ''
    this.proTypeParams.pheDescribe = ''
  }
  addseneorPhenItem() {
    let sensor = this.sensorPhenName
    if (!sensor || !this.proTypeParams.phenomenon) {
      this.jsonApiService.message('请选择传感器问题现象')
      return
    }
    let len = this.seneorPhenlist.length
    let itemId = 1
    if (len > 0) {
      itemId = this.seneorPhenlist[len - 1].index + 1
    }

    let phen =
      this.proSlectedItem.childs[+this.proTypeParams.problemClass].phenomenon[+this.proTypeParams.phenomenon].name
    let isAdd = true
    for (let item of this.seneorPhenlist) {
      if (item.phenomenon == phen) {
        let str = '传感器' + sensor
        if (str == item.sensorName) {
          isAdd = false
          break
        }
      }
    }
    if (isAdd) {
      let pheItem = {
        index: itemId,
        sensorName: '传感器' + sensor,
        phenomenon: phen
      }
      this.seneorPhenlist.push(pheItem)
    } else {
      this.jsonApiService.message('现象已存在,不需要重复添加')
    }
  }
  delSeneorPhenItem(index) {
    this.jsonApiService.confirm_message('确定删除该条现象?').then(() => {
      this.seneorPhenlist.splice(index, 1)
    })
  }
  getSelectedVAL() {
    var result: any = {
      problemTypeStr: this.proSlectedItem.name, //问题类型
      problemClassStr: '',
      phenomenonStr: '',
      repairGuideStr: '',
      repairWayStr: '',
      pheDescribeStr: '',
      resultPhenStr: ''
    }
    if (!this.proSlectedItem) {
      this.jsonApiService.message('请选择问题类型')
      return result
    }
    if (this.proTypeParams.problemClass) {
      //问题分类
      result.problemClassStr = this.proSlectedItem.childs[+this.proTypeParams.problemClass].name
    }
    if (this.proTypeParams.phenomenon) {
      //问题现象
      result.phenomenonStr =
        this.proSlectedItem.childs[+this.proTypeParams.problemClass].phenomenon[+this.proTypeParams.phenomenon].name
      result.resultPhenStr = result.phenomenonStr
    }
    if (this.proTypeParams.repairGuide) {
      //维修指导
      result.repairGuideStr =
        this.proSlectedItem.childs[+this.proTypeParams.problemClass].phenomenon[
          +this.proTypeParams.phenomenon
        ].repairGuide[+this.proTypeParams.repairGuide].name
    }
    if (this.proTypeParams.repairWay) {
      //维修方式
      result.repairWayStr =
        this.proSlectedItem.childs[+this.proTypeParams.problemClass].phenomenon[
          +this.proTypeParams.phenomenon
        ].repairWay[+this.proTypeParams.repairWay].name
    }
    if (this.proTypeParams.pheDescribe) {
      //维修描述
      result.pheDescribeStr =
        this.proSlectedItem.childs[+this.proTypeParams.problemClass].phenomenon[
          +this.proTypeParams.phenomenon
        ].pheDescribe[+this.proTypeParams.pheDescribe].name
    }

    if (+this.proTypeParams.problemClass == 1 && this.seneorPhenlist.length > 0) {
      let list = []
      for (let item of this.seneorPhenlist) {
        let str = item.sensorName + '-' + item.phenomenon
        list.push(str)
      }
      result.resultPhenStr = list.join(',')
    }
    if (this.pcIsOther) {
      result.resultPhenStr = this.otherInpMess
    }
    return result
  }

  upload() {
    $('#uploadImg').click()
  }
  // 上传图片
  uploadChange(event) {
    const fr = new FileReader()
    const imgItem = event.target.files[0]
    fr.readAsDataURL(imgItem)
    fr.onload = (ev: any) => {
      const imgFlag = this.imgList.some(item => item.name == imgItem.name)
      if (imgFlag) {
        this.jsonApiService.message('不能上传重复图片')
        $('#uploadImg').val('')
        return false
      }
      let imgUrl = ev.target.result
      this.imgList.push({ name: imgItem.name, imgUrl: imgUrl })
      this.imgFileList.push(imgItem)
    }
    $('#uploadImg').val('')
  }
  // 删除图片
  delImg(row) {
    this.imgList = this.imgList.filter(item => item.name != row.name)
    this.imgFileList = this.imgFileList.filter(item => item.name != row.name)
    $('#uploadImg').val('')
  }

  /*
	   新的图片上传  编辑页面
	*/

  imagesInfoAfter: any = [] //图片展示
  imagesInfoBefore: any = [] //图片展示
  imagesInfoOther: any = [] //图片展示
  isViewer: boolean = false //默认隐藏
  //显示图片组件
  viewer: any = ''
  //查看照片
  show_img(type, row) {
    //1主机 2采集器 3传感器 4线材 5GPS 6其他
    if (type == 'after') {
      this.imagesInfoAfter = JSON.parse(JSON.stringify(row))
      for (var i = 0; i < this.imagesInfoAfter.length; i++) {
        if (this.imagesInfoAfter[i].index == 1) {
          this.imagesInfoAfter[i].title = '主机'
        } else if (this.imagesInfoAfter[i].index == 2) {
          this.imagesInfoAfter[i].title = '采集器'
        } else if (this.imagesInfoAfter[i].index == 3) {
          this.imagesInfoAfter[i].title = '传感器'
        } else if (this.imagesInfoAfter[i].index == 4) {
          this.imagesInfoAfter[i].title = '线材'
        } else if (this.imagesInfoAfter[i].index == 5) {
          this.imagesInfoAfter[i].title = 'GPS'
        } else if (this.imagesInfoAfter[i].index == 6) {
          this.imagesInfoAfter[i].title = '其他'
        }
        this.imagesInfoAfter[i].localImg = []
        for (let c = 0; c < this.imagesInfoAfter[i].imageList.length; c++) {
          let localImg
          if (this.imagesInfoAfter[i].imageList[c].indexOf('http') == -1) {
            if (this.imagesInfoAfter[i].imageList[c] == '/') {
              localImg = config.BaseURL + this.imagesInfoAfter[i].imageList[c].substr(1)
            } else {
              localImg = config.BaseURL + this.imagesInfoAfter[i].imageList[c]
            }
          } else {
            localImg = this.imagesInfoAfter[i].imageList[c]
          }

          this.imagesInfoAfter[i].localImg.push(localImg)
        }
      }
      this.afterImageInfo = JSON.parse(JSON.stringify(this.imagesInfoAfter))
    }
    if (type == 'before') {
      this.imagesInfoBefore = JSON.parse(JSON.stringify(row))
      for (var j = 0; j < this.imagesInfoBefore.length; j++) {
        if (this.imagesInfoBefore[j].index == 1) {
          this.imagesInfoBefore[j].title = '主机'
        } else if (this.imagesInfoBefore[j].index == 2) {
          this.imagesInfoBefore[j].title = '采集器'
        } else if (this.imagesInfoBefore[j].index == 3) {
          this.imagesInfoBefore[j].title = '传感器'
        } else if (this.imagesInfoBefore[j].index == 4) {
          this.imagesInfoBefore[j].title = '线材'
        } else if (this.imagesInfoBefore[j].index == 5) {
          this.imagesInfoBefore[j].title = 'GPS'
        } else if (this.imagesInfoBefore[j].index == 6) {
          this.imagesInfoBefore[j].title = '其他'
        }
        this.imagesInfoBefore[j].localImg = []
        for (let a = 0; a < this.imagesInfoBefore[j].imageList.length; a++) {
          let localImg
          if (this.imagesInfoBefore[j].imageList[a].indexOf('http') == -1) {
            if (this.imagesInfoBefore[j].imageList[a] == '/') {
              localImg = config.BaseURL + this.imagesInfoBefore[j].imageList[a].substr(1)
            } else {
              localImg = config.BaseURL + this.imagesInfoBefore[j].imageList[a]
            }
          } else {
            localImg = this.imagesInfoBefore[j].imageList[a]
          }

          this.imagesInfoBefore[j].localImg.push(localImg)
        }
      }
      this.beforeImageInfo = JSON.parse(JSON.stringify(this.imagesInfoBefore))
    }

    if (type == 'other') {
      var otherImgObj = {
        localImg: [],
        imageList: row
      }
      for (let b = 0; b < row.length; b++) {
        let localImg
        if (row[b].indexOf('http') == -1) {
          if (row[b] == '/') {
            localImg = config.BaseURL + row[b].substr(1)
          } else {
            localImg = config.BaseURL + row[b]
          }
        } else {
          localImg = row[b]
        }

        otherImgObj.localImg.push(localImg)
      }

      this.otherImageInfo.push(otherImgObj)
    }
    if (this.isViewer && $('.viewer-container').length > 0) {
      this.viewer.destroy()
    }
  }
  //图片放大
  bigimg(type) {
    if (this.isViewer && $('.viewer-container').length > 0) {
      System.import('assets/js/viewer/viewer.js').then(() => {
        System.import('assets/js/viewer/bootstrap.bundle.min.js').then(() => {
          System.import('assets/js/viewer/viewer.css').then(() => {
            this.showImg(type)
          })
        })
      })
    } else {
      this.showImg(type)
    }
  }
  showImg(type) {
    var that = this
    var imgList
    if (this.pageType == 'edit') {
      if (type == 'after') {
        imgList = $('.afterImageInfo')[0]
      }
      if (type == 'before') {
        imgList = $('.beforeImageInfo')[0]
      }
      if (type == 'other') {
        imgList = $('.otherImageInfo')[0]
      }
      this.viewer = new Viewer(imgList, {
        url: 'src',
        title: function (image) {
          return image.alt + ' (' + (this.index + 1) + '/' + this.length + ')'
        }
      })
    } else {
      if (type == 'after') {
        imgList = $('.afterImage')[0]
      }
      if (type == 'before') {
        imgList = $('.beforeImage')[0]
      }
      if (type == 'other') {
        imgList = $('.otherImage')[0]
      }
      this.viewer = new Viewer(imgList, {
        url: 'src',
        title: function (image) {
          return image.alt + ' (' + (this.index + 1) + '/' + this.length + ')'
        }
      })
    }

    that.isViewer = true
  }

  /*
	   新的图片上传  新增页面
	*/
  imageInfo: any = [
    {
      Indexes: 0,
      index: 1,
      text: '主机',
      imgFileTitle: '点击上传',
      imageList: [],
      describe: 'host',
      localImg: []
    },
    {
      Indexes: 1,
      index: 2,
      text: '采集器',
      imgFileTitle: '点击上传',
      imageList: [],
      describe: 'collector',
      localImg: []
    },
    {
      Indexes: 2,
      index: 3,
      text: '传感器',
      imgFileTitle: '点击上传',
      imageList: [],
      describe: 'sensor',
      localImg: []
    },
    {
      Indexes: 3,
      index: 4,
      text: '线材',
      imgFileTitle: '点击上传',
      imageList: [],
      describe: 'wire',
      localImg: []
    },
    {
      Indexes: 4,
      index: 5,
      text: 'GPS',
      imgFileTitle: '点击上传',
      imageList: [],
      describe: 'GPS',
      localImg: []
    },
    {
      Indexes: 5,
      index: 6,
      text: '其他',
      imgFileTitle: '点击上传',
      imageList: [],
      describe: 'other',
      localImg: []
    }
  ] //图片List
  //删除
  deleteImg(type, index, i) {
    if (type == 'before') {
      this.beforeImageInfo[index - 1].imageList.splice(i, 1)
      this.beforeImageInfo[index - 1].localImg.splice(i, 1)
    } else {
      this.afterImageInfo[index - 1].imageList.splice(i, 1)
      this.afterImageInfo[index - 1].localImg.splice(i, 1)
    }

    if (this.isViewer && $('.viewer-container').length > 0) {
      this.viewer.destroy()
    }
  }
  fileList: any = []
  //上传多张
  multifileChange(type, index, files: FileList) {
    if (this.isViewer && $('.viewer-container').length > 0) {
      this.viewer.destroy()
    }
    var imgList = []
    var that = this
    let multfileList = files

    if (type == 'before') {
      imgList = JSON.parse(JSON.stringify(this.beforeImageInfo))
    } else {
      imgList = JSON.parse(JSON.stringify(this.afterImageInfo))
    }
    var reqdata = new FormData()
    reqdata.append('describe', imgList[index - 1].describe)
    reqdata.append('businessType', '2')
    reqdata.append('type', '2') //type 1获取相对路径  2获取绝对路径
    reqdata.append('deviceId', this.deviceId)

    for (var i = 0; i < multfileList.length; i++) {
      this.fileList = multfileList[i]
      const isJPG =
        this.fileList.type === 'image/jpeg' || this.fileList.type === 'image/jpg' || this.fileList.type === 'image/png'
      if (!isJPG) {
        this.jsonApiService.message('上传图片只能是 png, jpg, jpeg 格式!')
        return
      }

      reqdata.append('files', multfileList[i])
    }

    that.jsonApiService.form_http('fileUpload/upload.html', reqdata).subscribe(data => {
      // that.jsonApiService.message(data.message);
      if (data.success) {
        for (var i = 0; i < data.data.length; i++) {
          imgList[index - 1].imageList.push(data.data[i])
          let localImg
          if (data.data[i].indexOf('http') == -1) {
            if (data.data[i] == '/') {
              localImg = config.BaseURL + data.data[i].substr(1)
            } else {
              localImg = config.BaseURL + data.data[i]
            }
          } else {
            localImg = data.data[i]
          }
          imgList[index - 1].localImg.push(localImg)
        }

        if (type == 'before') {
          this.beforeImageInfo = JSON.parse(JSON.stringify(imgList))
        } else {
          this.afterImageInfo = JSON.parse(JSON.stringify(imgList))
        }
      }
    })

    for (let k = 0; k < $('.up').length; k++) {
      $('.up')[k].value = null
    }
  }
  //过滤图片数组值
  filter(value) {
    var imgList = []
    for (let i = 0; i < value.length; i++) {
      if (value[i].imageList.length != 0) {
        var img = {
          index: value[i].index,
          imageList: value[i].imageList
        }
        imgList.push(img)
      }
    }
    return imgList
  }
  //进厂限制:自定义限制,校验非空值,默认6小时,不允许为空
  inputChange() {
    this.isPushTime = Math.abs(Math.floor(this.isPushTime))
    if (!this.isPushTime) {
      this.isPushTime = 6
    }
  }
}
